//
//  ZZVisitorLoginView.swift
//  SinaWeibo
//
//  Created by QUAN on 15/11/9.
//  Copyright © 2015年 QUAN. All rights reserved.
//

import UIKit

@objc protocol ZZVisitorLoginViewDeleget : NSObjectProtocol {
    
    func visitorWillLogin()
    
    optional func visitorWillRegister()
}

class ZZVisitorLoginView: UIView {

    //声明代理 登录, 注册代理
    weak var visitorDelegate: ZZVisitorLoginViewDeleget?
    
    //实现代理方法
    @objc func visitorWillLogin() {
        
        visitorDelegate?.visitorWillLogin()
    }
    
    @objc func visitorWillRegister() {
        
        visitorDelegate?.visitorWillRegister!()
    }
    
    init() {
        super.init(frame: CGRectZero)
        //添加控件
        setUpUi()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
        
    }
    
    //切换界面, 改变文案, 和大头像
    func setUIInfo(imageName: String?, title: String) -> () {
    
        iconView.hidden = false
        tipLabel.text = title
        if imageName != nil {
            
            iconView.hidden = true
            bringSubviewToFront(circleView)
            circleView.image = UIImage(named: imageName!)
        }else{
            startAnimation()
        }
        
    }
    
    //圆圈动画
    private func startAnimation() {
        
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        anim.repeatCount = MAXFLOAT
        anim.toValue = 2 * M_PI
        anim.duration = 10
        anim.removedOnCompletion = false
        circleView.layer.addAnimation(anim, forKey: nil)
        
    }
    
    //控件约束
    private func setUpUi() {
        addSubview(circleView)
        addSubview(backView)
        addSubview(iconView)        
        addSubview(tipLabel)
        addSubview(loginBtn)
        addSubview(registerBtn)
        
        for v in subviews {
            v.translatesAutoresizingMaskIntoConstraints = false
        }
        
        //控件约束
        layoutView()
        
        self.loginBtn.addTarget(self, action: "visitorWillLogin", forControlEvents: .TouchUpInside)
        self.registerBtn.addTarget(self, action: "visitorWillRegister", forControlEvents: .TouchUpInside)
    }
    
    //懒加载控件
    //圆圈
    private lazy var circleView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    //大头像
    private lazy var iconView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    //蒙版
    private lazy var backView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    
    //文案
    private lazy var tipLabel: UILabel = UILabel(title: "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜", size: 14, color: UIColor.lightGrayColor(), alphaNum: nil)
    
    //登录按钮
    private lazy var loginBtn: UIButton = UIButton(title: "登录", image: nil, backImage: "common_button_white_disable", color: UIColor.lightGrayColor())
    
    //注册按钮
    private lazy var registerBtn: UIButton = UIButton(title: "注册", image: nil, backImage: "common_button_white_disable", color: themeColor)
    

    
    private func layoutView() {
        
        //圆圈约束
        circleView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self.snp_centerX)
            make.centerY.equalTo(self.snp_centerY).offset(-60)
        }
        
        //大图标的约束
        iconView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(circleView.snp_centerX)
            make.centerY.equalTo(circleView.snp_centerY)
        }
        
        //文案的约束
        tipLabel.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(circleView.snp_centerX)
            make.top.equalTo(circleView.snp_bottom)
            make.width.equalTo(220)
            make.height.equalTo(50)
        }
        
        //登录按钮
        loginBtn.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(tipLabel.snp_left)
            make.top.equalTo(tipLabel.snp_bottom).offset(16)
            make.width.equalTo(100)
            make.height.equalTo(35)
        }
        
        //注册按钮
        registerBtn.snp_makeConstraints { (make) -> Void in
            make.right.equalTo(tipLabel.snp_right)
            make.top.equalTo(tipLabel.snp_bottom).offset(16)
            make.width.equalTo(100)
            make.height.equalTo(35)
        }
        
        //蒙版
        backView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(self.snp_top)
            make.left.equalTo(self.snp_left)
            make.right.equalTo(self.snp_right)
            make.bottom.equalTo(registerBtn.snp_top)
            
            
        }
        
        //设置背景颜色
        backgroundColor = UIColor(white: 0.93, alpha: 1)
    }

}
